'use strict';

const Service = require('egg').Service;
const Op = require('sequelize').Op;

class ProductInfoService extends Service {
  async getProduct(name1) {
    const {
      ctx,
    } = this;
    const producttypeall = await ctx.model.Productinfo.findAll({
      where: {
        productname: {
          [Op.like]: `%${name1}%`,
        },
      },
      order: [
        [
          'id', 'ASC',
        ],
      ],
    });
    if (producttypeall) {
      return {
        status: 200,
        data: {
          product: producttypeall,
        },
      };
    }
    return {
      status: 400,
    };
  }
  async getProducts(name1, name2) {
    const {
      ctx,
    } = this;
    const producttypeall = await ctx.model.Productinfo.findAll({
      where: {
        productname: {
          [Op.like]: `%${name1}%`,
        },
        [Op.and]: {
          productname: {
            [Op.like]: `%${name2}%`,
          },
        },
      },
      order: [
        [
          'id', 'ASC',
        ],
      ],
    });
    if (producttypeall) {
      return {
        status: 200,
        data: {
          product: producttypeall,
        },
      };
    }
    return {
      status: 400,
    };
  }
  async selId(id) {
    const {
      ctx,
    } = this;
    const productId = await ctx.model.Productinfo.findByPk(id, {
      include: [{
        model: ctx.model.Productparameter,
      },
      {
        model: ctx.model.Productimg,
      },
      {
        model: ctx.model.Productevaluate,
      },
      ],
    });
    if (productId) {
      return {
        status: 200,
        data: {
          product: productId.dataValues,
        },
      };
    }
    return {
      status: 400,
    };
  }
  async getTypeProductInfo(name) {
    const {
      ctx,
    } = this;
    const result = await ctx.model.Productinfo.findAll({
      where: {
        [Op.or]: {
          explain: {
            [Op.like]: `%${name}%`,
          },
          productname: {
            [Op.like]: `%${name}%`,
          },
          type: {
            [Op.like]: `%${name}%`,
          },
        },
      },
      order: [
        [ 'createdDate', 'DESC' ],
      ],
    });
    if (result) {
      return {
        status: 200,
        data: {
          result,
        },
      };
    }
    return {
      status: 400,
      data: {
        msg: '数据获取失败请重试',
      },
    };
  }
  async getProductInfo(name, type) {
    const {
      ctx,
    } = this;
    const result = await ctx.model.Productinfo.findAll({
      where: {
        productname: {
          [Op.like]: `%${name}%`,
        },
        [Op.and]: {
          productname: {
            [Op.like]: `%${type}%`,
          },
        },
      },
      order: [
        [ 'createdDate', 'DESC' ],
      ],
    });
    if (result) {
      return {
        status: 200,
        data: {
          result,
        },
      };
    }
    return {
      status: 400,
    };
  }
}
module.exports = ProductInfoService;
